package com.laozhao.component;

import java.awt.Toolkit;
import java.io.File;
import java.net.URL;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.jdesktop.swingx.JXTitledPanel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.laozhao.msas.view.FileChooseAction;

import free.FreeUtil;

import twaver.TWaverUtil;


public class ViewUtil {

//	private static File xmlFile;
	protected static String NODE_NAME_TASKPLANE = "TaskPane";
	protected static String NODE_NAME_ACTION = "Action";
	protected static String NODE_NAME_TABPANEL = "Tab";
	protected static String NODE_ATTR_TEXT = "text";
	protected static String NODE_ATTR_TIP = "tooltip";
	protected static String NODE_ATTR_ICON = "icon";
	protected static String NODE_ATTR_ACTION = "action";
	protected static String NODE_ATTR_CLASS = "class";
	
	

	public static JXTaskPaneContainer loadTaskPaneContainer(String xmlFilePath,Class<? extends Action> actionClazz) {
		return loadTaskPaneContainer(getFile(xmlFilePath),actionClazz);
	}

	public static JXTaskPaneContainer loadTaskPaneContainer(File xmlFile,Class<? extends Action> actionClazz) {
//		xmlFile = xmlFile;
		JXTaskPaneContainer taskPaneContainer = null;
		try {
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
//			Document doc = db.parse(FreeUtil.class.getResource(xmlFile)
//					.openStream());
			Document doc = db.parse(xmlFile);

			Element root = doc.getDocumentElement();
			NodeList rootTasks = root.getChildNodes();
			if (rootTasks != null) {
				taskPaneContainer = new JXTaskPaneContainer();
				for (int i = 0; i < rootTasks.getLength(); i++) {
					org.w3c.dom.Node task = rootTasks.item(i);
					if (task.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						if (task.getNodeName().equalsIgnoreCase(NODE_NAME_TASKPLANE)) {
							String title = getStringAttribute(task, NODE_ATTR_TEXT);
							String tooltip = getStringAttribute(task, NODE_ATTR_TIP);
							Icon icon = getIconAttribute(task, NODE_ATTR_ICON);
							JXTaskPane taskPane = new JXTaskPane(title, icon);
							taskPane.setToolTipText(tooltip);
							
							NodeList actions = task.getChildNodes();
							 if (actions != null) {
						            for (int j = 0; j < actions.getLength(); j++) {
						                org.w3c.dom.Node itemNode = actions.item(j);
						                if (itemNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						                	if (itemNode.getNodeName().equalsIgnoreCase(NODE_NAME_ACTION)) {
						                		String name = getStringAttribute(itemNode, NODE_ATTR_TEXT);
												String shortDesc = getStringAttribute(itemNode, NODE_ATTR_TIP);
												Icon smallIcon = getIconAttribute(itemNode, NODE_ATTR_ICON);
						                		String commond = getStringAttribute(itemNode, NODE_ATTR_ACTION);
						                		Action action = actionClazz.newInstance();
						                		action.putValue(Action.NAME, name);
						                		action.putValue(Action.SHORT_DESCRIPTION, shortDesc);
						                		action.putValue(Action.SMALL_ICON, smallIcon);
						                		action.putValue(Action.ACTION_COMMAND_KEY, commond);
						                		
						                		taskPane.add((action));
						                		System.out
														.println("--"+taskPane.getTitle() +":"+ action.getValue(Action.NAME));
						                	}
						                }
						            }
							 }

							taskPaneContainer.add(taskPane);
	                		System.out
							.println("-"+taskPane.getTitle() +":"+ taskPaneContainer.getComponentCount());

						}
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return taskPaneContainer;
	}
	

	public static JTabbedPane loadTabbedPane(String xmlFilePath) {
		return loadTabbedPane(getFile(xmlFilePath));
	}
	
	public static JTabbedPane loadTabbedPane(File xmlFile) {
//		xmlFile = xmlFile;
		JTabbedPane tabbedPane = null;
		try {
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(xmlFile);

			Element root = doc.getDocumentElement();
			NodeList rootTabs = root.getChildNodes();
			if (rootTabs != null) {
				tabbedPane = new JTabbedPane();
				for (int i = 0; i < rootTabs.getLength(); i++) {
					org.w3c.dom.Node tab = rootTabs.item(i);
					if (tab.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						if (tab.getNodeName().equalsIgnoreCase(NODE_NAME_TABPANEL)) {
							String title = getStringAttribute(tab, NODE_ATTR_TEXT);
							String tooltip = getStringAttribute(tab, NODE_ATTR_TIP);
							Icon icon = getIconAttribute(tab, NODE_ATTR_ICON);
	                		String className = getStringAttribute(tab, NODE_ATTR_CLASS);
	                		Class<? extends JComponent> clazz = (Class<? extends JComponent>) Class.forName(className);
	                		JComponent component = clazz.newInstance();
	                		
	                		tabbedPane.addTab(title, icon, component, tooltip);	                		
						}
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return tabbedPane;
	}
	
//	public static JXTitledPanel load(String xmlFilePath) {
//		return loadTabbedPane(getFile(xmlFilePath));
//	}
//	
//	public static JTabbedPane loadTabbedPane(File xmlFile) {
////		xmlFile = xmlFile;
//		JTabbedPane tabbedPane = null;
//		try {
//			
//			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//			DocumentBuilder db = dbf.newDocumentBuilder();
//			Document doc = db.parse(xmlFile);
//
//			Element root = doc.getDocumentElement();
//			NodeList rootTabs = root.getChildNodes();
//			if (rootTabs != null) {
//				tabbedPane = new JTabbedPane();
//				for (int i = 0; i < rootTabs.getLength(); i++) {
//					org.w3c.dom.Node tab = rootTabs.item(i);
//					if (tab.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
//						if (tab.getNodeName().equalsIgnoreCase(NODE_NAME_TABPANEL)) {
//							String title = getStringAttribute(tab, NODE_ATTR_TEXT);
//							String tooltip = getStringAttribute(tab, NODE_ATTR_TIP);
//							Icon icon = getIconAttribute(tab, NODE_ATTR_ICON);
//	                		String className = getStringAttribute(tab, NODE_ATTR_CLASS);
//	                		Class<? extends JComponent> clazz = (Class<? extends JComponent>) Class.forName(className);
//	                		JComponent component = clazz.newInstance();
//	                		
//	                		tabbedPane.addTab(title, icon, component, tooltip);	                		
//						}
//					}
//				}
//			}
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//		return tabbedPane;
//	}
	
	public static File getFile(String path) {
		URL url = ViewUtil.class.getResource(path);
		return new File(url.getFile());
	}
	
	private static String getStringAttribute(org.w3c.dom.Node node,
			String name) {
		org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name);
		if (attribute != null) {
			return attribute.getNodeValue();
		} else {
			return null;
		}
	}

	private static Icon getIconAttribute(org.w3c.dom.Node node, String name) {
		String iconURL = getStringAttribute(node, name);
		if (iconURL != null && !iconURL.isEmpty()) {
			return TWaverUtil.getIcon(iconURL);
//			Icon icon = new ImageIcon(iconURL);
//			Icon icon = Toolkit.getDefaultToolkit().get
		}
		return null;
	}

	private static int getIntAttribute(org.w3c.dom.Node node, String name) {
		String value = getStringAttribute(node, name);
		if (value != null && !value.isEmpty()) {
			return Integer.valueOf(value);
		}
		return 0;
	}

}
